/* 
 * Copyright 2013 Sébastien Mouquet.
 * 
 * This file is part of ECovoiturage.
 * 
 * ECovoiturage is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ECovoiturage is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Foobar. If not, see http://www.gnu.org/licenses/.
 */
package com.smouquet.ecovoiturage.core.service.impl;

import static com.google.gwt.thirdparty.guava.common.base.Preconditions.checkArgument;

import com.smouquet.ecovoiturage.core.bean.Config;
import com.smouquet.ecovoiturage.core.exception.ConfigIOException;
import com.smouquet.ecovoiturage.core.service.ConfigService;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 *
 * @author sebastien.mouquet
 */
public class ConfigServiceImpl implements ConfigService
{

    @Override
    public String getConfigPath()
    {
        String configDir = System.getProperty(CONFIG_DIR_PROPERTY);
        
        StringBuilder configPath = new StringBuilder();
        configPath.append(configDir);
        configPath.append(File.separator);
        configPath.append(CONFIG_FILE_NAME);
        
        return configPath.toString();
    }

    @Override
    public void saveConfig(Config config) throws ConfigIOException
    {
        checkArgument(config != null, "config is required");
        
        
        try
        {
            Properties configProperties = new Properties();
            configProperties.setProperty(PROPERTY_JDBC_DRIVER, config.getJdbcDriver());
            configProperties.setProperty(PROPERTY_JDBC_URL, config.getJdbcUrl());
            configProperties.setProperty(PROPERTY_JDBC_USER, config.getJdbcUser());
            configProperties.setProperty(PROPERTY_JDBC_PASSWORD, config.getJdbcPassword());

            File configFile = new File(getConfigPath());
            FileOutputStream configOutput = new FileOutputStream(configFile);

            configProperties.store(configOutput, "ecovoiturage config file");
            
        } catch(FileNotFoundException ex) {
            throw new ConfigIOException("Config cannot be saved", ex);
        } catch(IOException ex) {
            throw new ConfigIOException("Config cannot be saved", ex);
        }
    }

    @Override
    public Config loadConfig() throws ConfigIOException
    {
        try
        {
            File configFile = new File(getConfigPath());
            if(!configFile.exists()) {
                return null;
            }

            FileInputStream configInput = new FileInputStream(configFile);

            Properties configProperties = new Properties();
            configProperties.load(configInput);
            
            Config config = new Config();
            config.setJdbcDriver(configProperties.getProperty(PROPERTY_JDBC_DRIVER));
            config.setJdbcUrl(configProperties.getProperty(PROPERTY_JDBC_URL));
            config.setJdbcUser(configProperties.getProperty(PROPERTY_JDBC_USER));
            config.setJdbcPassword(configProperties.getProperty(PROPERTY_JDBC_PASSWORD));
            
            return config;
            
        } catch(FileNotFoundException ex) {
            throw new ConfigIOException("Config cannot be loaded", ex);
        } catch(IOException ex) {
            throw new ConfigIOException("Config cannot be loaded", ex);
        }
    }
    
    
}
